package idiet.dao.hibernate;

import idiet.bean.Customer;
import idiet.bean.CustomerNurse;

import java.util.Date;
import java.util.List;

import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

@Repository("customerNurseDao")
public class CustomerNurseDao extends BaseDao{
	public CustomerNurse getCustomerNurseByCustomerNurseId(int customerNurseId){
		return (CustomerNurse) this.sessionFactory
				.openSession()
				.createCriteria(CustomerNurse.class)
				.add(Restrictions.eq("id", customerNurseId))
				.uniqueResult();
	}
	public CustomerNurse getCustomerNurse4LastByCustomer(Customer customer){
		Integer id=(Integer)this.sessionFactory
				.getCurrentSession()
				.createCriteria(CustomerNurse.class)
				.add(Restrictions.eq("customer", customer))
				.setProjection(Projections.max("id"))
				.uniqueResult();
		return this.getCustomerNurseByCustomerNurseId(id);
				
	}
	@SuppressWarnings("unchecked")
	public List<CustomerNurse> getCustomerNursesByCustomer(Customer customer){
		return this.sessionFactory
				.getCurrentSession()
				.createCriteria(CustomerNurse.class)
				.add(Restrictions.eq("customer", customer))
				//.addOrder(Order.desc("lmTimestamp"))
				.list();
	}
	@SuppressWarnings("unchecked")
	public List<CustomerNurse> getCustomerNurses(List<Customer> customers){
		return this.sessionFactory
				.getCurrentSession()
				.createCriteria(CustomerNurse.class)
				.add(Restrictions.in("customer", customers))
				.list();
	}
	@SuppressWarnings("unchecked")
	public List<CustomerNurse> getCustomerNurses(){
		return this.sessionFactory
				.getCurrentSession()
				.createCriteria(CustomerNurse.class)
				.list();
	}
	@SuppressWarnings("unchecked")
	public List<CustomerNurse> getCustomerNurses(Customer customer,Date lmTimestampStart,Date lmTimestampEnd){
		return this.sessionFactory
				.getCurrentSession()
				.createCriteria(CustomerNurse.class)
				.add(Restrictions.between("lmTimestamp", lmTimestampStart, lmTimestampEnd))
				.add(Restrictions.eq("customer", customer))
				.list();
	}
	@SuppressWarnings("unchecked")
	public List<CustomerNurse> getCustomerNurses(List<Customer> customers,Date lmTimestampStart,Date lmTimestampEnd){
		return this.sessionFactory
				.getCurrentSession()
				.createCriteria(CustomerNurse.class)
				.add(Restrictions.between("lmTimestamp", lmTimestampStart, lmTimestampEnd))
				.add(Restrictions.in("customer", customers))
				.list();
	}
	@SuppressWarnings("unchecked")
	public List<CustomerNurse> getCustomerNurses(Date lmTimestampStart,Date lmTimestampEnd){
		return this.sessionFactory
				.getCurrentSession()
				.createCriteria(CustomerNurse.class)
				.add(Restrictions.between("lmTimestamp", lmTimestampStart, lmTimestampEnd))
				.list();
	}
	public void createCustomerNurse(CustomerNurse customerNurse){
		this.sessionFactory
		.getCurrentSession()
		.save(customerNurse);
	}
}
